*******************************************************************************
* Purpose: generates labor markets outcomes at weekly level for both parents
* Outcomes: UI benefit, sick leave, self sufficient, parental leave
*******************************************************************************


do "D:\Data\workdata\708296\Project - Mother groups and mental health\Do\global.do" 

//Create panel of DREAM data for mother
use "$work\dreammothers",clear // created in 5_cr_dream
* parental leave benefits = 881, 2015 53
forvalues i = 12/20 {
forvalues j = 1/9	{
rename y_`i'0`j' y20`i'`j'	
}
}
forvalues i = 12/20 {
forvalues j = 10/52	{
rename y_`i'`j' y20`i'`j'	
}
}
rename y_1553 y201553		
bys id_mother mfr_dob: keep if _n == 1
reshape long y, i(id_mother mfr_dob) j(time)
save "$work\dreammotherspanel", replace


//Create panel of DREAM data for father
use "$work\dreamfathers",clear // created in 5_cr_dream
* barselsdagpenge = 881, 2015 53
forvalues i = 12/20 {
forvalues j = 1/9	{
rename y_`i'0`j' y20`i'`j'	
}
}
forvalues i = 12/20 {
forvalues j = 10/52	{
rename y_`i'`j' y20`i'`j'	
}
}
rename y_1553 y201553	
bys id_father mfr_dob: keep if _n == 1
reshape long y, i(id_father mfr_dob) j(time)
save "$work\dreamfatherspanel", replace


//Unemployment benefits
use "$work\dreammotherspanel",clear
gen ui = 0
replace ui = 1 if y == "111" | y == "115" | y == "121" ///
| y == "151" | y == "152" | y == "153" ///
| y == "130" | y == "133" | y == "134" | y == "135" | y == "136" | y == "137" | y == "138" | y == "139" ///
| y == "213" | y == "214" | y == "215" | y == "216" | y == "217" | y == "218" | y == "219" | y == "299" ///
| y == "710" | y == "713" | y == "714" | y == "715" | y == "716" | y == "717" | y == "718" | y == "719" ///
| y == "730" | y == "733" | y == "734" | y == "735" | y == "736" | y == "737" | y == "738" | y == "739" ///
| y == "740" | y == "743" | y == "744" | y == "745" | y == "746" | y == "747" | y == "748" | y == "749" ///
	
tostring time, replace
gen y_b = substr(time,1,4)
gen u_b = substr(time,5,2)

destring y_b, replace
destring u_b, replace
drop time
sort id_mother mfr_dob y_b u_b
by id_mother mfr_dob: gen time =_n

gen y_ = year(mfr_dob)
gen u = week(mfr_dob)

gen treat_time = .
replace treat_time = time if y_b == y_ & u_b == u
sort id_mother mfr_dob treat_time
by id_mother mfr_dob: carryforward treat_time, replace

sort id_mother mfr_dob time
gen t = time - treat_time
keep if t >= 0 & t < 5*52

keep id_mother mfr_dob ui t 
reshape wide ui, i(id_mother mfr_dob) j(t)

compress
save "$work\UIbenefits", replace

use "$work\dreamfatherspanel",clear
gen uif = 0
replace uif = 1 if y == "111" | y == "115" | y == "121" ///
| y == "151" | y == "152" | y == "153" ///
| y == "130" | y == "133" | y == "134" | y == "135" | y == "136" | y == "137" | y == "138" | y == "139" ///
| y == "213" | y == "214" | y == "215" | y == "216" | y == "217" | y == "218" | y == "219" | y == "299" ///
| y == "710" | y == "713" | y == "714" | y == "715" | y == "716" | y == "717" | y == "718" | y == "719" ///
| y == "730" | y == "733" | y == "734" | y == "735" | y == "736" | y == "737" | y == "738" | y == "739" ///
| y == "740" | y == "743" | y == "744" | y == "745" | y == "746" | y == "747" | y == "748" | y == "749" ///
	
tostring time, replace
gen y_b = substr(time,1,4)
gen u_b = substr(time,5,2)

destring y_b, replace
destring u_b, replace
drop time
sort id_father mfr_dob y_b u_b
by id_father mfr_dob: gen time =_n

gen y_ = year(mfr_dob)
gen u = week(mfr_dob)

gen treat_time = .
replace treat_time = time if y_b == y_ & u_b == u
sort id_father mfr_dob treat_time
by id_father mfr_dob: carryforward treat_time, replace

sort id_father mfr_dob time
gen t = time - treat_time
keep if t >= 0 & t < 5*52

keep id_father mfr_dob ui t 
reshape wide ui, i(id_father mfr_dob) j(t)

compress
save "$work\UIbenefitsf", replace


//Sick leave
use "$work\dreammotherspanel",clear
gen sick = 0
replace sick = 1 if y == "890" | y == "893" | y == "894" | y == "895" | y == "896" | y == "897" | y == "898" | y == "899"

tostring time, replace
gen y_b = substr(time,1,4)
gen u_b = substr(time,5,2)

destring y_b, replace
destring u_b, replace
drop time
sort id_mother mfr_dob y_b u_b
by id_mother mfr_dob: gen time =_n

gen y_ = year(mfr_dob)
gen u = week(mfr_dob)

gen treat_time = .
replace treat_time = time if y_b == y_ & u_b == u
sort id_mother mfr_dob treat_time
by id_mother mfr_dob: carryforward treat_time, replace

sort id_mother mfr_dob time
gen t = time - treat_time
keep if t >= 0 & t < 5*52

keep id_mother mfr_dob sick t 
reshape wide sick, i(id_mother mfr_dob) j(t)

compress
save "$work\sick", replace

//Parental leave
use "$work\dreammotherspanel",clear
gen barsel = 0
replace barsel = 1 if y == "881"

tostring time, replace
gen y_b = substr(time,1,4)
gen u_b = substr(time,5,2)

destring y_b, replace
destring u_b, replace
drop time
sort id_mother mfr_dob y_b u_b
by id_mother mfr_dob: gen time =_n

gen y_ = year(mfr_dob)
gen u = week(mfr_dob)

gen treat_time = .
replace treat_time = time if y_b == y_ & u_b == u
sort id_mother mfr_dob treat_time
by id_mother mfr_dob: carryforward treat_time, replace

sort id_mother mfr_dob time
gen t = time - treat_time
keep if t >= 0 & t < 5*52

keep id_mother mfr_dob barsel t 
reshape wide barsel, i(id_mother mfr_dob) j(t)

compress
save "$work\barsel", replace

// working (self sufficient)
use "$work\dreammotherspanel",clear
gen work = 0
replace work = 1 if y == ""

tostring time, replace
gen y_b = substr(time,1,4)
gen u_b = substr(time,5,2)

destring y_b, replace
destring u_b, replace
drop time
sort id_mother mfr_dob y_b u_b
by id_mother mfr_dob: gen time =_n

gen y_ = year(mfr_dob)
gen u = week(mfr_dob)

gen treat_time = .
replace treat_time = time if y_b == y_ & u_b == u
sort id_mother mfr_dob treat_time
by id_mother mfr_dob: carryforward treat_time, replace

sort id_mother mfr_dob time
gen t = time - treat_time
keep if t >= 0 & t < 5*52

keep id_mother mfr_dob work t 
reshape wide work, i(id_mother mfr_dob) j(t)

compress
save "$work\work", replace
//sick leave father
use "$work\dreamfatherspanel",clear

gen sick = 0
replace sick = 1 if y == "890" | y == "893" | y == "894" | y == "895" | y == "896" | y == "897" | y == "898" | y == "899"

tostring time, replace
gen y_b = substr(time,1,4)
gen u_b = substr(time,5,2)

destring y_b, replace
destring u_b, replace
drop time
sort id_father mfr_dob y_b u_b
by id_father mfr_dob: gen time =_n

gen y_ = year(mfr_dob)
gen u = week(mfr_dob)

gen treat_time = .
replace treat_time = time if y_b == y_ & u_b == u
sort id_father mfr_dob treat_time
by id_father mfr_dob: carryforward treat_time, replace

sort id_father mfr_dob time
gen t = time - treat_time
keep if t >= 0 & t < 5*52

keep id_father mfr_dob sick t 
rename sick sickf
reshape wide sick, i(id_father mfr_dob) j(t)

compress
save "$work\sickf", replace

//Parental leave father
use "$work\dreamfatherspanel",clear
gen barsel = 0
replace barsel = 1 if y == "881"

tostring time, replace
gen y_b = substr(time,1,4)
gen u_b = substr(time,5,2)

destring y_b, replace
destring u_b, replace
drop time
sort id_father mfr_dob y_b u_b
by id_father mfr_dob: gen time =_n

gen y_ = year(mfr_dob)
gen u = week(mfr_dob)

gen treat_time = .
replace treat_time = time if y_b == y_ & u_b == u
sort id_father mfr_dob treat_time
by id_father mfr_dob: carryforward treat_time, replace

sort id_father mfr_dob time
gen t = time - treat_time
keep if t >= 0 & t < 5*52

keep id_father mfr_dob barsel t 
rename barsel barself
reshape wide barsel, i(id_father mfr_dob) j(t)

compress
save "$work\barself", replace

//Work (self sufficient) father
use "$work\dreamfatherspanel",clear
gen work = 0
replace work = 1 if y == ""

tostring time, replace
gen y_b = substr(time,1,4)
gen u_b = substr(time,5,2)

destring y_b, replace
destring u_b, replace
drop time
sort id_father mfr_dob y_b u_b
by id_father mfr_dob: gen time =_n

gen y_ = year(mfr_dob)
gen u = week(mfr_dob)

gen treat_time = .
replace treat_time = time if y_b == y_ & u_b == u
sort id_father mfr_dob treat_time
by id_father mfr_dob: carryforward treat_time, replace

sort id_father mfr_dob time
gen t = time - treat_time
keep if t >= 0 & t < 5*52

keep id_father mfr_dob work t 
rename work workf
reshape wide work, i(id_father mfr_dob) j(t)

compress
save "$work\workf", replace

** New employment outcomes 
use "$work\dreammothers_branche",clear
forvalues i = 2010/2020 {
forvalues j = 1/9	{
rename branche_`i'_0`j' branche`i'`j'	
}
}
forvalues i = 2010/2020 {
forvalues j = 10/12	{
rename branche_`i'_`j' branche`i'`j'	
}
}	
bys id_mother mfr_dob: keep if _n == 1
reshape long branche, i(id_mother mfr_dob) j(time)
compress
save "$work\dreammotherspanel_branche", replace

use "$work\dreamfathers_branche",clear
forvalues i = 2010/2020 {
forvalues j = 1/9	{
rename branche_`i'_0`j' branche`i'`j'	
}
}
forvalues i = 2010/2020 {
forvalues j = 10/12	{
rename branche_`i'_`j' branche`i'`j'	
}
}	
bys id_father mfr_dob: keep if _n == 1
reshape long branche, i(id_father mfr_dob) j(time)
compress
save "$work\dreamfatherspanel_branche", replace

use "$work\dreammotherspanel_branche",clear
gen empl = 0
replace empl = 1 if branche != ""

tostring time, replace
gen y_b = substr(time,1,4)
gen u_b = substr(time,5,2)

destring y_b, replace
destring u_b, replace
drop time
sort id_mother mfr_dob y_b u_b
by id_mother mfr_dob: gen time =_n

gen y_ = year(mfr_dob)
gen u = month(mfr_dob)

gen treat_time = .
replace treat_time = time if y_b == y_ & u_b == u
sort id_mother mfr_dob treat_time
by id_mother mfr_dob: carryforward treat_time, replace

sort id_mother mfr_dob time
gen t = time - treat_time
keep if t >= 0 & t < 5*12

keep id_mother mfr_dob empl t 
reshape wide empl, i(id_mother mfr_dob) j(t)
compress
save "$work\employment", replace

use "$work\dreamfatherspanel_branche",clear
gen emplf = 0
replace emplf = 1 if branche != ""

tostring time, replace
gen y_b = substr(time,1,4)
gen u_b = substr(time,5,2)

destring y_b, replace
destring u_b, replace
drop time
sort id_father mfr_dob y_b u_b
by id_father mfr_dob: gen time =_n

gen y_ = year(mfr_dob)
gen u = month(mfr_dob)

gen treat_time = .
replace treat_time = time if y_b == y_ & u_b == u
sort id_father mfr_dob treat_time
by id_father mfr_dob: carryforward treat_time, replace

sort id_father mfr_dob time
gen t = time - treat_time
keep if t >= 0 & t < 5*12

keep id_father mfr_dob emplf t 
reshape wide emplf, i(id_father mfr_dob) j(t)
compress
save "$work\employment_f", replace